﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;

public partial class SysManage_RolesM_BWebRolesGroupAndColumn : INTPage
{
    SqlHelp help = new SqlHelp();
    CommonMethod comHelp = new CommonMethod();
    private string errorTimeOutUrl = "../../error.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, errorTimeOutUrl);
        if (!IsPostBack)
        {
            //权限
            comHelp.GetRightUnite(this, new string[] { Session["UserCode"].ToString(), Session["CoCode"].ToString(), "webRolesGroupAndColumn" },
                new string[] { "Button1", "Button2", "Button3", "Button4" }, new int[] { 1, 3, 3, 1 }, "../../SLogin.aspx");

            //绑定页面组
            RolesGroup();
        }
    }

    #region 初始化绑定 WebAndGroup(string UserCode)    
    
    // 绑定角色组
    protected void RolesGroup()
    {
        string StrWebGroupSql = " select RG.RGCode,RG.RGName from B_RolesGroup RG where RG.DeleteFlag<>1 order by RG.RGCode ";
        ddlRolesGroupName.DataTextField = "RGName";
        ddlRolesGroupName.DataValueField = "RGCode";
        ddlRolesGroupName.DataSource = help.GetDataTable(StrWebGroupSql);
        ddlRolesGroupName.DataBind();

        ColumnNotInGroupBind(ddlRolesGroupName.SelectedValue.ToString());
    }

    #endregion

    //分别绑定未在此页面角色组组的字段和在页面角色组的字段的列表
    protected void ColumnNotInGroupBind(string RGCode)
    {
        //未在组里的
        string StrWNGSql = string.Format(" select  RC.ColumnType,convert(varchar,RC.ColumnPosition)+'---'+RC.ColumnType+'---'+RC.ColumnName ColumnName from B_Roles_Column RC where RC.ColumnType not in (select RG.ColumnType from B_RolesGroupAndColumn RG where RG.RGCode='{0}') order by RC.ColumnPosition", RGCode);
        lbxRolesNotInGroup.DataSource = help.GetDataTable(StrWNGSql);
        lbxRolesNotInGroup.DataValueField = "ColumnType";
        lbxRolesNotInGroup.DataTextField = "ColumnName";
        lbxRolesNotInGroup.DataBind();

        //在组里的
        string StrWGSql = string.Format(" select  RC.ColumnType,convert(varchar,RC.ColumnPosition)+'---'+RC.ColumnType+'---'+RC.ColumnName ColumnName from B_Roles_Column RC where RC.ColumnType in (select RG.ColumnType from B_RolesGroupAndColumn RG where RG.RGCode='{0}') order by RC.ColumnPosition", RGCode);
        DataTable Dt = help.GetDataTable(StrWGSql);
        lbxRolesInGroup.DataSource = Dt;
        lbxRolesInGroup.DataValueField = "ColumnType";
        lbxRolesInGroup.DataTextField = "ColumnName";
        lbxRolesInGroup.DataBind();

        lbxInsert2.DataSource = Dt;
        lbxInsert2.DataValueField = "ColumnType";
        lbxInsert2.DataTextField = "ColumnName";
        lbxInsert2.DataBind();
    }

    //根据页面组动态绑定
    protected void ddlWebGroupName_TextChanged(object sender, EventArgs e)
    {
        lbxInsert.Items.Clear();
        lbxInsert2.Items.Clear();
        lbxDelete.Items.Clear();
        ColumnNotInGroupBind(ddlRolesGroupName.SelectedValue.ToString());
    }

    #region 页面分组
    //单项添加
    protected void Button1_Click(object sender, EventArgs e)
    {
        //不在组里的页面数
        //int InNum = lbxWebNotInGroup.Items.Count;
        for (int i = 0; i < lbxRolesNotInGroup.Items.Count; i++)
        {
            if (lbxRolesNotInGroup.SelectedIndex >= 0)
            {
                lbxRolesInGroup.Items.Add(lbxRolesNotInGroup.SelectedItem);
                //是否插入
                int INum = 0;
                string StrColumn = lbxRolesNotInGroup.SelectedItem.Text.ToString();

                for (int d = 0; d < lbxInsert2.Items.Count; d++)
                {
                    if (StrColumn.Equals(lbxInsert2.Items[d].Text.ToString()))
                    { ++INum; }
                }

                if (INum < 1)
                { lbxInsert.Items.Add(lbxRolesNotInGroup.SelectedItem); }

                lbxRolesNotInGroup.Items.Remove(lbxRolesNotInGroup.SelectedItem);
                lbxDelete.Items.Remove(lbxRolesNotInGroup.SelectedItem);
            }
        }
    }

    //单项移除
    protected void Button2_Click(object sender, EventArgs e)
    {
        //不在组里的页面数
        //int InNum = lbxWebInGroup.Items.Count;
        for (int i = 0; i < lbxRolesInGroup.Items.Count; i++)
        {
            if (lbxRolesInGroup.SelectedIndex >= 0)
            {
                lbxRolesNotInGroup.Items.Add(lbxRolesInGroup.SelectedItem);
                //插入行
                int INum = 0;
                //页面类型
                string ColumnStr = lbxRolesInGroup.SelectedItem.Text.ToString();

                for (int k = 0; k < lbxInsert2.Items.Count; k++)
                {
                    if (ColumnStr.Equals(lbxInsert2.Items[k].Text.ToString()))
                    {
                        ++INum;
                    }
                }
                if (INum > 0)
                {
                    lbxDelete.Items.Add(lbxRolesInGroup.SelectedItem);
                }
                lbxInsert.Items.Remove(lbxRolesInGroup.SelectedItem);
                lbxRolesInGroup.Items.Remove(lbxRolesInGroup.SelectedItem);
            }
        }
    }

    //全部添加
    protected void Button4_Click(object sender, EventArgs e)
    {
        //在删除组的
        for (int j = 0; j < lbxDelete.Items.Count; j++)
        {
            lbxRolesNotInGroup.Items.Remove(lbxDelete.Items[j]);
            lbxRolesInGroup.Items.Add(lbxDelete.Items[j]);
        }
        //不在组里的页面数
        for (int i = 0; i < lbxRolesNotInGroup.Items.Count; i++)
        {
            lbxRolesInGroup.Items.Add(lbxRolesNotInGroup.Items[i]);
            lbxInsert.Items.Add(lbxRolesNotInGroup.Items[i]);
        }
        lbxRolesNotInGroup.Items.Clear();
        lbxDelete.Items.Clear();
    }
    //全部删除
    protected void Button3_Click(object sender, EventArgs e)
    {
        //在添加组的
        for (int k = 0; k < lbxInsert.Items.Count; k++)
        {
            lbxRolesNotInGroup.Items.Add(lbxInsert.Items[k]);
            lbxRolesInGroup.Items.Remove(lbxInsert.Items[k]);
        }
        //不在组里的页面数
        for (int i = 0; i < lbxRolesInGroup.Items.Count; i++)
        {
            lbxRolesNotInGroup.Items.Add(lbxRolesInGroup.Items[i]);
            lbxDelete.Items.Add(lbxRolesInGroup.Items[i]);
        }
        lbxRolesInGroup.Items.Clear();
        lbxInsert.Items.Clear();
    }
    #endregion

    //保存
    protected void cmdSave_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, errorTimeOutUrl);
        string Str = string.Empty;//操作字符串
        //页面编号
        string RGCode = ddlRolesGroupName.SelectedItem.Value.ToString();
        //DataTable Dt = (DataTable)Session["InGroup_Dt"];
        //当前用户
        string UserName = Session["UserCode"].ToString();
        //当前日期
        string StrDate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");
        //统计插入与删除的行数
        int InsertN = lbxInsert.Items.Count;
        int DeleteN = lbxDelete.Items.Count;

        //插入
        if (InsertN > 0)
        {   //插入SQL语句前部分
            string InsertStr = " Insert Into B_RolesGroupAndColumn(DeleteFlag,AddAuthor,"
            + "AddDate,UpdateAuthor,UpdateDate,RGCode,ColumnType) values('0','" + UserName
            + "','" + StrDate + "','" + UserName + "','" + StrDate + "','" + RGCode + "','";

            for (int i = 0; i < InsertN; i++)
            {
                Str += InsertStr + lbxInsert.Items[i].Value.ToString() + "');  ";
            }
        }
        //删除
        if (DeleteN > 0)
        {   //删除SQL语句前部分
            string DeleteStr = " Delete from B_RolesGroupAndColumn where RGCode='"
                + RGCode + "' and ColumnType='";
            for (int j = 0; j < DeleteN; j++)
            {
                Str += DeleteStr + lbxDelete.Items[j].Value.ToString() + "';   ";
            }
        }

        if (!string.IsNullOrEmpty(Str))
        {
            if (help.RunSqlInt(Str) > 0)
            {
                CommonMethod.regAlertMessage("操作成功！", this);
                ColumnNotInGroupBind(ddlRolesGroupName.SelectedItem.Value.ToString());
            }
        }
    }
   
    //查询
    protected void cmdQuery_Click(object sender, EventArgs e)
    {
        lbxRolesNotInGroup.Items.Clear();
        //未在组里的
        string StrWNGSql = string.Format(" select RC.ColumnType,RC.ColumnName from B_Roles_Column RC where RC.ColumnType not in ("
            + " select RG.ColumnType from B_RolesGroupAndColumn RG where RG.RGCode='{0}') and (RC.ColumnType like '%{1}%' OR RC.ColumnName like '%{2}%')", ddlRolesGroupName.SelectedValue.ToString(), txtCondition.Text.ToString(), txtCondition.Text.ToString());
        lbxRolesNotInGroup.DataSource = help.GetDataTable(StrWNGSql);
        lbxRolesNotInGroup.DataValueField = "ColumnType";
        lbxRolesNotInGroup.DataTextField = "ColumnName";
        lbxRolesNotInGroup.DataBind();
    }
    //导出Excel
    protected void cmdExport_Click(object sender, EventArgs e)
    {
        //在组里的
        string StrWGSql = " select RC.ColumnType,RC.ColumnName from B_Roles_Column RC where RC.ColumnType in ("
            + " select RG.ColumnType from B_RolesGroupAndColumn RG where RG.RGCode='"
            + ddlRolesGroupName.SelectedValue.ToString() + "')";
        DataTable Dt = help.GetDataTable(StrWGSql);
        //Export("application/ms-excel", "信息表.xls", Dt);
        string path = Server.MapPath("ExproteTempFile") + @"\信息表.xls";
        //Response.Write(path);
        //HWobject.ExportExcel(Dt, path);
        //Response.Redirect("FrmDownFile.aspx?url=" + path);
    }
    private void Export(string FileType, string FileName, DataTable Dt)
    {
        GridView ExcelGrv = new GridView();
        ExcelGrv.DataSource = Dt;
        ExcelGrv.DataBind();
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        ExcelGrv.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }
}
